package com.welab.pandahelper.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.welab.pandahelper.bean.ExpandableListItem;
import com.welab.pandahelper.bean.FastJsonUtils;
import com.welab.pandahelper.bean.MyResponse;
import com.welab.pandahelper.config.MyConfig;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

public class codeUtils {
    private static Gson gson = new Gson();
    private static String codeType;
    private static String phone;
    private static Boolean sendStatus = false;
    private static Boolean getTokenStatus = false;
    private static SharedPreferences mPrefs;

    public static boolean pandaSendCode(String phone){
        String url  = "https://www.oextool.com/api/phoneGetvalidatecode.do?phone=" + phone;
        try {
            Response response = httpUtils.get(url);
            if (response.code() == 200){
                MyResponse j = gson.fromJson(response.body().string(), MyResponse.class);
                Log.e(MyConfig.APP_NAME,j.getMsg());
            }
        } catch (IOException e) {
            Log.e(MyConfig.APP_NAME,"网络请求异常！");
            e.printStackTrace();
        }
        return true;
    }

//    public static boolean


    public static void sendCode(Context context){
        sendStatus = false;
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                String success = (String) msg.obj;
                Toast.makeText(context,"短信发送：" + success ,Toast.LENGTH_LONG).show();
            }
        };

        new Thread(){
            @Override
            public void run() {
                Boolean sendStatus = false;
                Message message = new Message();
                switch (codeType){
                    case "panda":{
                        sendStatus = pandaSendCode(phone);
                        break;
                    }
                }
                if (sendStatus){
                    message.obj = "success";
                }else{
                    message.obj = "false";
                }
                handler.sendMessage(message);
            }
        }.start();
    }

    public static boolean checkPandaToken(Context context,String phone){
        mPrefs = context.getSharedPreferences("panda_token", Context.MODE_PRIVATE);
        String token = mPrefs.getString(phone,"0");
        String url = "https://www.oextool.com/api/orderList_on.do?giftId=&pageIndex=1&apptoken=" + token;
        new Thread(){
            @Override
            public void run() {
                Message message = new Message();
                try {
                    Response response = httpUtils.get(url);
                    if (response.code() == 200){
                        MyResponse j = gson.fromJson(response.body().string(), MyResponse.class);
                        if(j.getSuccess()){
                            configUtils.putStrToSharedPreferences(mPrefs,phone+"_flag","1");
                        }else {
                            configUtils.putStrToSharedPreferences(mPrefs,phone+"_flag","0");
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        return false;
    }




    public static boolean getPandaToken(Context context,String code,String phone){
        String url = "https://www.oextool.com/api/loginByVcode.do?vcode=" + code + "&phone=" + phone;
        try {
            Response response = httpUtils.get(url);
            if (response.code() == 200){
                MyResponse j = gson.fromJson(response.body().string(), MyResponse.class);
                Map<String,String> data = (Map<String, String>) j.getData();
                Log.e(MyConfig.APP_NAME,j.getMsg());
                Log.e(MyConfig.APP_NAME,j.toString());
                if (data != null){
                    String appToken = data.get("apptoken");
                    Log.e(MyConfig.APP_NAME,appToken);
                    mPrefs = context.getSharedPreferences("panda_token", Context.MODE_PRIVATE);
                    configUtils.putStrToSharedPreferences(mPrefs,phone,appToken);
                    configUtils.putStrToSharedPreferences(mPrefs,phone+"_flag","1");
                }else {
                    Log.e(MyConfig.APP_NAME,"data is null");
                    Log.e(MyConfig.APP_NAME,url);
                }
                return true;
            }
        } catch (IOException e) {
            Log.e(MyConfig.APP_NAME,"网络请求异常！");
            e.printStackTrace();
        }
        return true;
    }

    public static boolean getCatToken(Context context,String code,String phone){
        String url = "https://h5.meowstarke.com/tao/user/login";
        try {
            Map<String,String> user = new HashMap<String, String>();
            Map<String,String> head = new HashMap<String, String>();
            user.put("pwd",code);
            user.put("phone",phone);
            String userJson = FastJsonUtils.objectToJson(user);
            Response response = httpUtils.post(url,userJson,null);
            if (response.code() == 200){
                MyResponse j = gson.fromJson(response.body().string(), MyResponse.class);
                Map<String,String> data = (Map<String, String>) j.getData();
                Log.e(MyConfig.APP_NAME,j.getMsg());
                if (data != null){
                    String appToken = data.get("uuid");
                    Log.e(MyConfig.APP_NAME,appToken);
                    mPrefs = context.getSharedPreferences("cat_token", Context.MODE_PRIVATE);
                    configUtils.putStrToSharedPreferences(mPrefs,phone,appToken);
                }else {
                    Log.e(MyConfig.APP_NAME,"data is null");
                    Log.e(MyConfig.APP_NAME,url);
                }
                return true;
            }
        } catch (IOException e) {
            Log.e(MyConfig.APP_NAME,"网络请求异常！");
            e.printStackTrace();
        }
        return true;
    }




    public static void getToken(Context context,String code,String phone){
        sendStatus = false;
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                String success = (String) msg.obj;
                Toast.makeText(context,"认证：" + success ,Toast.LENGTH_LONG).show();
            }
        };

        new Thread(){
            @Override
            public void run() {
                Boolean getTokenStatus = false;
                Message message = new Message();
                switch (codeType){
                    case "panda":{
                        getTokenStatus = getPandaToken(context,code,phone);
                        break;
                    }
                    case "cat":{
                        getTokenStatus = getCatToken(context,code,phone);
                        break;
                    }
                }
                if (getTokenStatus){
                    message.obj = "success";
                }else{
                    message.obj = "false";
                }
                handler.sendMessage(message);
            }
        }.start();

    }

    public static void setCodeType(String codeType1){
        codeType = codeType1;
    }

    public static void setPhone(String phone1){
        phone = phone1;
    }

    public static boolean getSendStatus() {
        return sendStatus;
    }
}
